package cn.yj.relay.core.service.impl;

import cn.yj.relay.common.utils.DateTimeUtils;
import cn.yj.relay.common.utils.StringUtils;
import cn.yj.relay.core.entity.Task;
import cn.yj.relay.core.mapper.TaskMapper;
import cn.yj.relay.core.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 永健
 * @since 2020-03-03 17:41
 */
@Service
@Transactional
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Override
    public Page<Task> findList(Map<String, Object> map, Page<Task> page) {
        return page.setRecords(baseMapper.findList(map, page));
    }

    @Override
    public Map<Object, Object> countDataByEmpId(Integer userId) {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;

        Map<Object, Object> map = new HashMap<>();
        List<Object> list = new LinkedList<>();
        List<Object> morthList = new LinkedList<>();

        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotNull(userId)) {
            params.put("empId", userId);
        }

        for (int i = 1; i <= month; i++) {
            Date start = DateTimeUtils.getFirstDayOfMonth(i);
            Date end = DateTimeUtils.getLastDayOfMonth(i);
            params.put("start", start);
            params.put("end", end);
            double all = baseMapper.countAll(params);
            map.put(i + "月", all);
            morthList.add(i + "月");
            list.add(all);
        }
        map.put("list", list);
        map.put("morthlist", morthList);

        return map;
    }

    @Override
    public Map<Object, Object> countDataAllUser() {
        return countDataByEmpId(null);
    }

    @Override
    public List<Map<String, Object>> sumEmpRoYalMoney(Date startTime, Date endTIme, Integer empId) {
        // emp_phone
        return baseMapper.sumEmpRoYalMoney(startTime, endTIme, empId);
    }
}
